package com.hamilton.erik.pt.mobile.android.widget;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hamilton.erik.pt.core.model.Feeding;
import com.hamilton.erik.pt.mobile.android.R;

public class FeedingListAdapter extends BaseAdapter {

	private List<Feeding> feedings;
	private LayoutInflater layoutInflater;
	
	public FeedingListAdapter(Context context, List<Feeding> feedings) {
		this.feedings = feedings;
		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return feedings.size();
	}

	@Override
	public Object getItem(int index) {
		return feedings.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		FeedingHolder feedingHolder;
		
		if(convertView == null) {
			convertView = layoutInflater.inflate(R.layout.feeding_row_view, null);
			
			feedingHolder = new FeedingHolder();
			
			feedingHolder.time = (TextView)convertView.findViewById(R.id.feeding_time);
			feedingHolder.item = (TextView)convertView.findViewById(R.id.feeding_item);
			
			convertView.setTag(feedingHolder);
		}
		else {
			feedingHolder = (FeedingHolder)convertView.getTag();
		}
		
		feedingHolder.time.setText(feedings.get(position).getTimestamp());
		feedingHolder.item.setText(feedings.get(position).getFeedingItem());
		
		return convertView;
	}

	static class FeedingHolder {
		TextView item;
		TextView time;
	}
}
